/**
 * @名称: msg,alert,confirm,loading,toast model of layer
 * @更新: 2021.01.27
 * @作者: yuguoliang
 * @博客：http://www.cssw3c.com
 */
;(function ($, layer, document, undefined) {
    $.msg = function (opt) {
        var defaults = {
            anim: 'up',
            direct: 'middle',
            msg: '操作成功',
            shade: false,
            shadeClose: false,
            time: 1.5,
            type: 1,
        };
        var opts = {};
        if (typeof opt === 'string') {
            opts.msg = opt;
        } else {
            opts = opt;
        }
        var config = $.extend({}, defaults, opts);

        var html, className, style = '';
        if (config.direct === 'middle') {
            className = 'weui-toast-msg';
            style = '';
            html = '<span class="weui-tmsg">' + config.msg + '</span>';
        } else if (config.direct === 'bottom') {
            className = 'weui-toast-msg weui-bottom';
            html = '<span class="weui-tmsg">' + config.msg + '</span>';
        }
        style += ' ' + config.style;
        layer.open({
            anim: config.anim,
            className: className,
            content: html,
            style: style,
            shade: config.shade,
            shadeClose: config.shadeClose,
            type: config.type,
            time: config.time,
        });
    };

    $.alert = function (opt) {
        var defaults = {
            title: null,
            msg: '',
            className:'',
            style: 'width: 80%;max-width: 300px;',
            skin: 'weui-dialog',
            shade: true,
            shadeClose: false,
            type: 1, //防止和$.loading()冲突
            touchNo: true,
            zIndex:-1,
            btn: ['确定'],
            onOK: function (i) {
                layer.close(i);
            },
            onCancel: function (i) {
                layer.close(i);
            },
        };
        var opts = {};
        if (typeof opt === 'string') {
            opts.msg = opt;
        } else {
            opts = opt;
        }
        var config = $.extend({}, defaults, opts);
        var html = '<div class="' + (config.skin + '_bd') + '">' + config.msg + '</div>';
        layer.open({
            title: config.title,
            content: html,
            className: config.title ? config.className : config.className + ' title-no',
            style: config.style,
            skin: config.skin,
            shade: config.shade,
            shadeClose: config.shadeClose,
            type: config.type,
            touchNo: config.touchNo,
            zIndex: config.zIndex,
            btn: config.btn,
            yes: config.onOK,
            no: config.onCancel,
        });
    };

    $.confirm = function (opt) {
        var defaults = {
            title: '',
            msg: '',
            style: 'width: 80%;max-width: 300px;',
            skin: 'weui-dialog',
            shade: true,
            shadeClose: false,
            type: 1, //防止和$.loading()冲突
            touchNo: true,
            btn: ['确定', '取消'],
            onOK: function (i) {
                layer.close(i);
            },
            onCancel: function (i) {
                layer.close(i);
            },
        };
        var config = $.extend({}, defaults, opt);
        var html = '<div class="' + (config.skin + '_bd') + '">' + config.msg + '</div>';
        layer.open({
            title: config.title,
            content: html,
            className: config.title ? '' : 'title-no',
            style: config.style,
            skin: config.skin,
            shade: config.shade,
            shadeClose: config.shadeClose,
            type: config.type,
            touchNo: config.touchNo,
            btn: config.btn,
            yes: config.onOK,
            no: config.onCancel,
        });
    };

    $.loading = function (opt) {
        var defaults = {
            className: 'weui-toast',
            msg: '数据加载中',
            type: 0,
            shade: 'background-color: rgba(0,0,0,0);',
            shadeClose: false,
            touchNo: true,
        };
        var opts = {};
        if (typeof opt === 'string') {
            opts.msg = opt;
        } else {
            opts = opt;
        }
        var config = $.extend({}, defaults, opts);
        var html = '';
        html += '<i class="weui-loading weui-icon_toast"></i>';
        html += '<p class="weui-toast__content">' + config.msg + '</p>';
        return layer.open({
            className: config.className,
            content: html,
            type: config.type,
            shade: config.shade,
            shadeClose: config.shadeClose,
            touchNo: config.touchNo,
        });
    };

    //toast 用于临时显示某些信息，并且会在数秒后自动消失。这些信息通常是轻量级操作的成功、失败或等待状态信息。
    $.toast = function (opt) {
        var defaults = {
            className: 'weui-toast',
            msg: '',
            shade: 'background-color: rgba(0,0,0,0);',
            shadeClose: false,
            skin: 'success',
            time: 2,
            touchNo: true,
            callback: null,
        };
        var opts = {};
        if (typeof opt === 'string') {
            opts.msg = opt;
        } else {
            opts = opt;
        }
        var config = $.extend({}, defaults, opts);
        var skinMsg = config.skin === 'success' ? '操作成功' : '操作失败';
        var classIco;
        switch (config.skin) {
            case 'success':
                classIco = 'weui-icon-success-no-circle';
                break;
            case 'error':
                classIco = 'weui-icon-cancel';
                break;
            default:
                classIco = 'weui-icon-success-no-circle';
                break;
        }
        var html = '';
        html += '<i class="' + classIco + ' weui-icon_toast"></i>';
        html += '<p class="weui-toast__content">' + (config.msg ? config.msg : skinMsg) + '</p>';
        layer.open({
            className: config.className,
            content: html,
            shade: config.shade,
            shadeClose: config.shadeClose,
            time: config.time,
            touchNo: config.touchNo,
            end: function () {
                if ($.isFunction(config.callback)) {
                    config.callback.call(this);
                }
            }
        });
    };
})($, layer, document);
